﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Tierless.Framework.Constraints
{
	/// <summary>
	/// Represents a list of constraint violations.
	/// </summary>
	public class ConstraintViolations : List<ConstraintViolation>
	{
		/// <summary>
		/// Gets the combined messages for all violations in the list.
		/// </summary>
		public string CombinedMessage
		{
			get
			{
				var message = new StringBuilder();
				foreach (var violation in this)
				{
					if (message.Length > 0)
						message.Append(Environment.NewLine);
					message.Append(violation.Message);
				}
				return message.ToString();
			}
		}

		/// <summary>
		/// Gets the combined error messages for all violations with error severity in the list.
		/// </summary>
		public string CombinedErrorMessage
		{
			get
			{
				var message = new StringBuilder();
				foreach (var violation in this.Where(AViolation => AViolation.Severity == ViolationSeverity.Error))
				{
					if (message.Length > 0)
						message.AppendLine();
					message.Append(violation.Message);
				}
				return message.ToString();
			}
		}

		/// <summary>
		/// Gets the combined warning messages for all violations with warning severity in the list.
		/// </summary>
		public string CombinedWarningMessage
		{
			get
			{
				var message = new StringBuilder();
				foreach (var violation in this.Where(AViolation => AViolation.Severity == ViolationSeverity.Warning))
				{
					if (message.Length > 0)
						message.AppendLine();
					message.Append(violation.Message);
				}
				return message.ToString();
			}
		}

		/// <summary>
		/// Determines whether the list has any errors.
		/// </summary>
		/// <returns>True if the list has any violations with error severity, false otherwise.</returns>
		public bool HasErrors()
		{
			return this.Any(AViolation => AViolation.Severity == ViolationSeverity.Error);
		}
	}
}